package com.party.admin.task;

import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.PackageCount;
import com.party.core.model.system.TargetType;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IPackageCountService;
import com.party.core.service.notify.IInstanceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author： yifeng
 * @description： 机构短信使用数量校正
 * @date：Created in  2018/01/16 下午 18:00
 **/
@Service(value = "qartzOrgSmsRevise")
public class QuartzOrgSmsRevise {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    public IInstanceService instanceService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private IPackageCountService packageCountService;

    public void run() {
        List<MemberGroup> members = memberGroupService.getAllPartnerNoOpenStatus();
        for (MemberGroup member : members) {
            PackageCount unique = packageCountService.getUnique(new PackageCount(member.getId(), TargetType.PUSH_SMS.getCode()));
            if (unique != null) {
                Integer useNum = instanceService.getSmsSendSuccess(member.getId());
                if (!useNum.equals(unique.getUseNum())) {
                    unique.setUseNum(useNum);
                    packageCountService.update(unique);
                }
            }
        }
    }
}
